import axios from "./axios_config";

/**
 * 获取货代数据
 */
export function getFreightForwardData() {
  return new Promise((resolve, reject) => {
    axios.get('/freightForward/select').then(res => {
      if (res.data) {
        var data = JSON.parse(res.data);
        if (data.errCode === 10000) {
          resolve(data.content); // 将获取到的数据赋值给menuItems
        } else {
          reject(new Error(data.errMsg));
        }
      }
    }).catch(error => {
      reject(new Error('列表数据获取失败!'));
    });
  });
}

/**
 * 获取渠道数据
 */
export function getChannelData(forwardId, currentPage, pagesize) {
  return new Promise((resolve, reject) => {
    axios.get('/channel/select/' + forwardId + '/' + currentPage + '/' + pagesize).then(res => {
      if (res.data) {
        var data = {};
        var list = [];
        if (typeof res.data === 'string') {
          data = JSON.parse(res.data);
        } else {
          data = res.data;
        }
        if (data.errCode === 10000) {
          let ListArray = []
          for (let key in data.content) {
            if (key === 'totalCount') {
              if (currentPage <= 1) {
                list['totalCount'] = data.content.totalCount;
              }
              continue;
            }
            ListArray.push(data.content[key])
          }
          list['list'] = ListArray;
          resolve(list); // 将获取到的数据赋值给menuItems
        } else {
          reject(new Error('获取菜单数据失败'));
        }
      }
    })
  });

}
